/**
 *  BasicBlock.java : IceBird project
 *  19:42:51 
 */
package icebird.compiler.bytecode;

import java.util.ArrayList;

/**
 * @author Sergey Shulepoff[Knott]
 */
public final class BasicBlock {
	
	private int start, end;
	private boolean exception;
	
	private TypeStack entryStack;
	
	private ArrayList<BasicBlock> entries;

	/**
	 * @param start
	 * @param end
	 * @param exception
	 */
	public BasicBlock(int start, int end, boolean exception) {
		entries = new ArrayList<BasicBlock>();
		
		this.start = start;
		this.end = end;
		this.exception = exception;
	}
	
	public BasicBlock(int start) {
		this(start, -1, false);
	}

	/**
	 * @return the end
	 */
	public final int getEndPC() {
		return end;
	}

	/**
	 * @return the start
	 */
	public final int getStartPC() {
		return start;
	}

	/**
	 * @param end the end to set
	 */
	public final void setEndPC(int end) {
		this.end = end;
	}

	/**
	 * @param start the start to set
	 */
	public final void setStartPC(int start) {
		this.start = start;
	}

	/**
	 * @return the exception
	 */
	public final boolean isException() {
		return exception;
	}

	/**
	 * @param exception the exception to set
	 */
	public final void setException(boolean exception) {
		this.exception = exception;
	}
	
	public final boolean contains(int address) {
		return ((address >= start) && (address < end));
	}

	/**
	 * @return the entryStack
	 */
	public final TypeStack getEntryStack() {
		return entryStack;
	}

	/**
	 * @param entryStack the entryStack to set
	 */
	public final void setEntryStack(TypeStack entryStack) {
		this.entryStack = entryStack;
	}
	
	public void addEntry(BasicBlock block) {
		if (block == null) return;
		
		entries.add(block);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append("START: ");
		buf.append(start);
		buf.append(" END: ");
		buf.append(end);
		buf.append(" EX: ");
		buf.append(exception);
		return buf.toString();
	}
}
